#include <stdio.h>
#include <string.h>

/**
 * The unsigned integer pow() function
 */
unsigned int custom_pow(unsigned int base, unsigned int exp)
{
	unsigned int i, re = 1;
	for (i=0; i<exp; i++) re = re * base;
	return re;
}

int main(int argc, char *argv[])
{
	int n;
	char alien[128], source[128], target[128], re[128];
	char *p, ch;
	unsigned int alen, slen, tlen, d;
	unsigned int i, case_num = 1;
	scanf("%d", &n);
	while (n--) {
		scanf("%s %s %s", alien, source, target);

		alen = strlen(alien);
		slen = strlen(source);
		tlen = strlen(target);

		d = 0; /* Initialize the weighted sum. */

		/* Reverse the alien number squence. */
		for (i=0; i<alen/2; i++)
		{
			ch = alien[i];
			alien[i] = alien[alen-i-1];
			alien[alen-i-1] = ch;
		}
		
		/* Calculate the weighted sum. */
		for (i=0; i<alen; i++) {
			p = strchr(source, alien[i]);
			d = d + (p - source) * custom_pow(slen, i);
		}

		/* Calculate the remainders. */
		i = 0;
		while (d / tlen) {
			re[i] = target[d % tlen];
			d = d / tlen; i++;
		}
		re[i] = target[d % tlen];

		/* Print the result. */
		printf("Case #%d: %c", case_num, re[i]);
		while (i--) {
			printf("%c", re[i]);
		}
		printf("\n");
		case_num++;
	}

	return 0;
}
